import cv2 as cv
import sys
import os
from python_ai.common.xcommon import *
import matplotlib.pyplot as plt
import numpy as np
import time
import datetime


def my_show_img(img, title="no title", trans=None, **kwargs):
    global spn
    spn += 1
    plt.subplot(spr, spc, spn)
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)
    plt.axis('off')
    plt.title(title)


img_dir = '../../../../../large_data/pic'
img_name = 'opencv_logo.png'
# img_name = 'IMG_20200819_124310.jpg'
# img_name = 'HSV.jpg'
img_path1 = os.path.join(img_dir, img_name)

spr = 2
spc = 2
spn = 0
plt.figure(figsize=[7, 7])

sep('load')
img = cv.imread(img_path1, cv.IMREAD_COLOR)
print('original shape', img.shape)
my_show_img(img, 'original', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

sep('Gaussian')
gau = cv.GaussianBlur(img, (5, 5), 0)
my_show_img(gau, 'blur', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

sep('Gaussian kernel')
kernel = cv.getGaussianKernel(5, 0)
print(kernel)
filtered = cv.filter2D(img, cv.CV_32F, kernel)
print_numpy_ndarray_info(filtered, 'filtered')
filtered = np.uint8(filtered)
print_numpy_ndarray_info(filtered, 'filtered')
my_show_img(filtered, 'filtered', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

plt.show()
